#include <iostream>

#include <QFileDialog>
#include <QMessageBox>
#include <QX11Info>

#include "qpowerboxserver.h"

#include <X11/Xlib.h>


QPowerBoxServer::QPowerBoxServer()
	: QObject(), display(QX11Info::display())
{
}



bool QPowerBoxServer::confirmOverwrite(QString filename, long long parent)
{
    QMessageBox dialog;
	dialog.setWindowModality(Qt::WindowModal);
	dialog.setAttribute(Qt::WA_X11BypassTransientForHint);
	XSetTransientForHint(display, dialog.winId(), parent);

	dialog.setWindowTitle("Allow Overwrite?");
	dialog.setIcon(QMessageBox::Question);
	dialog.setText("Do you want to overwrite '" + filename + "'?");
	dialog.setStandardButtons(QMessageBox::Yes | QMessageBox::No);

	dialog.exec();
	return (dialog.standardButton(dialog.clickedButton()) == QMessageBox::Yes);
}



QStringList QPowerBoxServer::fileDialog(
	int operation, QString title, long long parent,
	QString startDir, QString filter, bool allowMultipleSelections)
{
	QFileDialog dialog(NULL, title, startDir, filter);
	dialog.setWindowModality(Qt::WindowModal);
	dialog.setAttribute(Qt::WA_X11BypassTransientForHint);
	XSetTransientForHint(display, dialog.winId(), parent);

	switch(operation)
	{
		case OPEN_FILE:
			if(allowMultipleSelections)
				dialog.setFileMode(QFileDialog::ExistingFiles);

			else
				dialog.setFileMode(QFileDialog::ExistingFile);

			break;

		case SAVE_FILE:
			dialog.setFileMode(QFileDialog::AnyFile);
			break;

		case SELECT_DIR:
			dialog.setFileMode(QFileDialog::DirectoryOnly);
			break;
	}

	if(dialog.exec()) return dialog.selectedFiles();
	else return QStringList();
}

